const express = require('express');
const app = express()
const vipLogin = require('../data/login.json');
const logOut = require('../data/logout.json');
const routerData = require('../data/getRouter.json');
app.use(express.urlencoded({ extended: true }))
app.use(express.json())

app.use(function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*")
    res.header("Access-Control-Allow-Methods", "PUT  , POST , DELETE")
    res.header("Access-Control-Allow-Headers", "X-Requersted-With")
    res.header("Access-Control-Allow-Headers", "Content-Type")
    next()
})
const ERROR = {
    1: {
        "code": 200,
        "message": "账号错误",
        "success": false
    },
    2: {
        "code": 200,
        "message": "密码错误",
        "success": false
    }
}

app.post("/getlogin", (req, res) => {
    console.log(req.body, 'req');
    const { userName, password } = req.body
    if (userName && password) {
        if (userName !== 'admin') return res.send(ERROR[1])
        if (password !== '1234') return res.send(ERROR[2])
        else return res.send(vipLogin)
    }
})

app.post("/logOut", (req, res) => {
    console.log(req.body, 'req');
    res.send(logOut)
})


app.post("/getRouter", (req, res) => {
    console.log(req.body, 'req');
    res.send(routerData)
})

app.listen(3033, () => {
    console.log('服务器运行在3033');
})